package com.edgar.common.lib.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by edgar on 2015/11/14.
 */
public abstract class CommonAdapter<T> extends BaseAdapter{

    private List<T> mList = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mInflater;

    public CommonAdapter(Context context){
        this.mContext = context;
    }

    public void addList(Collection<T> collection){
        mList.addAll(collection);
    }

    public void addObject(T object){
        mList.add(object);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        if(position >= 0 && position < getCount()){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.bindViewHolder(mContext,convertView,position,getLayoutId());
        onBindView(viewHolder,getItem(position));
        return viewHolder.getConnerView();
    }

    protected abstract void onBindView(ViewHolder viewHolder,T object);

    protected abstract int getLayoutId();
}
